using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DataTablePage : System.Web.UI.Page
{
    string connectionString =
    WebConfigurationManager.ConnectionStrings[
    "RCVDB2000"].ConnectionString;
    SqlConnection sqlConnection;
    SqlCommand sqlCommand;
    SqlDataAdapter sqlDataAdapter;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }
    void LoadData()
    {
        sqlConnection = new SqlConnection();
        sqlConnection.ConnectionString =
        connectionString;
        try
        {
            sqlConnection.Open();
            sqlCommand = new SqlCommand();
            sqlCommand.CommandText =
            "select * from tblProvinces";
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandType = CommandType.Text;
            sqlDataAdapter
            = new SqlDataAdapter(sqlCommand);
            DataTable dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable);
            FillDataTabletToDropDonList(dataTable);
            sqlCommand.Dispose();
            sqlConnection.Close();
        }
        catch (Exception ex)
        {
            Literal1.Text += "Failed:" + ex.Message;
        }
        sqlConnection.Dispose();
    }

    void FillDataTabletToDropDonList(DataTable dataTable)
    {
        DropDownList1.DataSource = dataTable;
        DropDownList1.DataTextField = "ProvinceName";
        DropDownList1.DataValueField = "ProvinceID";
        DropDownList1.DataBind();       
    }
}
